﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="CuitBrowserWindow.cs" company="">
//   
// </copyright>
// <summary>
//   The cuit browser window.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace UITest.CUIT.CuitControls.HtmlControls
{
    using System;

    using Microsoft.VisualStudio.TestTools.UITesting;

    using UITest.CUIT.CuitControls.Helper;

    /// <summary>
    /// The cuit browser window.
    /// </summary>
    public class CuitBrowserWindow : BrowserWindow
    {
        #region Fields

        /// <summary>
        ///     The browsers helper.
        /// </summary>
        public BrowsersHelper browsersHelper = new BrowsersHelper();

        #endregion

        #region Constructors and Destructors

        /// <summary>
        ///     Initializes a new instance of the <see cref="CuitBrowserWindow" /> class.
        /// </summary>
        public CuitBrowserWindow()
            : this(string.Empty)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="CuitBrowserWindow"/> class.
        /// </summary>
        /// <param name="title">
        /// The title.
        /// </param>
        public CuitBrowserWindow(string title)
        {
            this.SetWindowTitle(title);
        }

        #endregion

        #region Public Properties

        /// <summary>
        ///     Gets or sets the window title.
        /// </summary>
        public string WindowTitle { get; set; }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The browser clear cache and cookies.
        /// </summary>
        /// <param name="browsers">
        /// The browsers.
        /// </param>
        public void BrowserClearCacheAndCookies(Browsers browsers)
        {
            this.browsersHelper.BrowserClearCacheAndCookies(browsers);
        }

        /// <summary>
        /// The get.
        /// </summary>
        /// <param name="searchProperties">
        /// The search properties.
        /// </param>
        /// <typeparam name="T">
        /// </typeparam>
        /// <returns>
        /// The <see cref="T"/>.
        /// </returns>
        public T Get<T>(string searchProperties = null) where T : ICuitControlBase
        {
            return this.CreateASTControls<T>(searchProperties);
        }

        /// <summary>
        ///     The get browser window.
        /// </summary>
        /// <returns>
        ///     The <see cref="BrowserWindow" />.
        /// </returns>
        public BrowserWindow GetBrowserWindow()
        {
            return this;
        }

        /// <summary>
        /// The kill browser process.
        /// </summary>
        /// <param name="browsers">
        /// The browsers.
        /// </param>
        public void KillBrowserProcess(Browsers browsers)
        {
            this.browsersHelper.KillBrowserProcess(browsers);
        }

        /// <summary>
        /// The set browser window.
        /// </summary>
        /// <param name="testControl">
        /// The test control.
        /// </param>
        public void SetBrowserWindow(UITestControl testControl)
        {
            Locate(testControl.TopParent.Name);
        }

        /// <summary>
        /// The set browser window.
        /// </summary>
        /// <param name="title">
        /// The title.
        /// </param>
        public void SetBrowserWindow(string title)
        {
            Locate(title);
        }

        /// <summary>
        /// The set window title.
        /// </summary>
        /// <param name="title">
        /// The title.
        /// </param>
        public void SetWindowTitle(string title)
        {
            this.WindowTitles.Clear();
            this.WindowTitles.Add(title);
            this.WindowTitle = title;
        }

        /// <summary>
        /// The start browser.
        /// </summary>
        /// <param name="browsers">
        /// The browsers.
        /// </param>
        /// <param name="uri">
        /// The uri.
        /// </param>
        /// <returns>
        /// The <see cref="BrowserWindow"/>.
        /// </returns>
        public BrowserWindow StartBrowser(Browsers browsers, string uri)
        {
            return this.browsersHelper.StartBrowser(browsers, uri);
        }

        #endregion

        #region Methods

        private T CreateASTControls<T>(string searchProperties)
        {
            object htmlControl;

            if (typeof(T) == typeof(CuitHtmlButton))
            {
                htmlControl = new CuitHtmlButton(this, searchProperties);
            }
            else if (typeof(T) == typeof(CuitHtmlCheckBox))
            {
                htmlControl = new CuitHtmlCheckBox(this, searchProperties);
            }
            else if (typeof(T) == typeof(CuitHtmlComboBox))
            {
                htmlControl = new CuitHtmlComboBox(this, searchProperties);
            }
            else if (typeof(T) == typeof(CuitHtmlDiv))
            {
                htmlControl = new CuitHtmlDiv(this, searchProperties);
            }
            else if (typeof(T) == typeof(CuitHtmlEdit))
            {
                htmlControl = new CuitHtmlEdit(this, searchProperties);
            }
            else if (typeof(T) == typeof(CuitHtmlEditableDiv))
            {
                htmlControl = new CuitHtmlEditableDiv(this, searchProperties);
            }
            else if (typeof(T) == typeof(CuitHtmlEditUnderDiv))
            {
                htmlControl = new CuitHtmlEditUnderDiv(this, searchProperties);
            }
            else if (typeof(T) == typeof(CuitHtmlFileInput))
            {
                htmlControl = new CuitHtmlFileInput(this, searchProperties);
            }
            else if (typeof(T) == typeof(CuitHtmlHyperLink))
            {
                htmlControl = new CuitHtmlHyperLink(this, searchProperties);
            }
            else if (typeof(T) == typeof(CuitHtmlImage))
            {
                htmlControl = new CuitHtmlImage(this, searchProperties);
            }
            else if (typeof(T) == typeof(CuitHtmlInputButton))
            {
                htmlControl = new CuitHtmlInputButton(this, searchProperties);
            }
            else if (typeof(T) == typeof(CuitHtmlLabel))
            {
                htmlControl = new CuitHtmlLabel(this, searchProperties);
            }
            else if (typeof(T) == typeof(CuitHtmlList))
            {
                htmlControl = new CuitHtmlList(this, searchProperties);
            }
            else if (typeof(T) == typeof(CuitHtmlRadioButton))
            {
                htmlControl = new CuitHtmlRadioButton(this, searchProperties);
            }
            else if (typeof(T) == typeof(CuitHtmlSpan))
            {
                htmlControl = new CuitHtmlSpan(this, searchProperties);
            }
            else if (typeof(T) == typeof(CuitHtmlEditableSpan))
            {
                htmlControl = new CuitHtmlEditableSpan(this, searchProperties);
            }
            else if (typeof(T) == typeof(CuitHtmlTable))
            {
                htmlControl = new CuitHtmlTable(this, searchProperties);
            }
            else if (typeof(T) == typeof(CuitHtmlCell))
            {
                htmlControl = new CuitHtmlCell(this, searchProperties);
            }
            else if (typeof(T) == typeof(CuitHtmlTextArea))
            {
                htmlControl = new CuitHtmlTextArea(this, searchProperties);
            }
            else if (typeof(T) == typeof(CuitHtmlIFrame))
            {
                htmlControl = new CuitHtmlIFrame(this, searchProperties);
            }
            else if (typeof(T) == typeof(CuitHtmlCustom))
            {
                htmlControl = new CuitHtmlCustom(this, searchProperties);
            }
            else
            {
                throw new Exception(string.Format("CreateControls: '{0}' not supported", typeof(T).Name));
            }

            return (T)htmlControl;
        }

        #endregion
    }
}